package com.emokili.ewa.core.frame.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emokili.ewa.common.entity.BaseEntity;
import com.emokili.ewa.core.frame.dao.FrameDao;
import com.emokili.ewa.core.frame.dao.MemberFrameDao;
import com.emokili.ewa.core.frame.entity.*;
import com.emokili.ewa.core.frame.service.*;
import com.emokili.ewa.core.frame.vo.FrameDetailVo;
import com.emokili.ewa.core.frame.vo.FrameOnlineStatusVo;
import com.emokili.ewa.core.frame.vo.FrameOutVo;
import com.emokili.ewa.core.frame.vo.FrameVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FrameServiceImpl extends ServiceImpl<FrameDao, FrameEntity> implements FrameService {

    @Autowired
    private MemberFrameService memberFrameService;
    @Autowired
    private FrameModelService frameModelService;
    @Autowired
    private FramePlayListService framePlayListService;
    @Autowired
    private MemberService memberService;

    @Override
    public void update(FrameVo frameVo) {
        FrameEntity frameEntity = new FrameEntity();
        BeanUtils.copyProperties(frameVo,frameEntity);
        updateById(frameEntity);
    }

    @Override
    public List<FrameOutVo> list(Long memberId) {
        List<MemberFrameEntity> memberFrameEntities = memberFrameService.list(new QueryWrapper<MemberFrameEntity>().eq("member_id", memberId));
        List<FrameOutVo> frameOutVoList = memberFrameEntities.stream().map(memberFrameEntity -> {
            FrameOutVo frameOutVo = new FrameOutVo();
            String serialNumber = memberFrameEntity.getSerialNumber();
            FrameEntity frameEntity = getOne(new QueryWrapper<FrameEntity>().eq("serial_number", serialNumber));
            BeanUtils.copyProperties(frameEntity, frameOutVo);
            Integer modelId = frameEntity.getModelId();
            FrameModelEntity frameModelEntity = frameModelService.getById(modelId);
            frameOutVo.setModel(frameModelEntity.getModel());
            List<Long> playlistIds = framePlayListService.list(new QueryWrapper<FramePlayListEntity>()
                    .eq("frame_id", frameEntity.getId())).stream().map(item -> {
                return item.getPalyListId();
            }).collect(Collectors.toList());
            frameOutVo.setPlaylists(playlistIds);
            return frameOutVo;
        }).collect(Collectors.toList());
        return frameOutVoList;
    }

    /**
     * 获取相框是否在线状态
     */
    @Override
    public  List<FrameOnlineStatusVo>  getOnlineStatus(Long memberId) {
        List<MemberFrameEntity> memberFrameEntities = memberFrameService.list(new QueryWrapper<MemberFrameEntity>().eq("member_id", memberId));
        List<FrameOnlineStatusVo> frameOnlineStatusVoList = memberFrameEntities.stream().map(memberFrameEntity -> {
            FrameOnlineStatusVo frameOnlineStatusVo = new FrameOnlineStatusVo();
            String serialNumber = memberFrameEntity.getSerialNumber();
            FrameEntity frameEntity = getOne(new QueryWrapper<FrameEntity>().eq("serial_number", serialNumber));
            frameOnlineStatusVo.setFrameId(frameEntity.getId());
            frameOnlineStatusVo.setLastConnected(frameEntity.getLastConnected());
            frameOnlineStatusVo.setLastSeen(frameEntity.getLastSeen());
            frameOnlineStatusVo.setCompatibility(frameEntity.getCompatibility() == 0 ? false : true);
            return frameOnlineStatusVo;
        }).collect(Collectors.toList());
        return frameOnlineStatusVoList;
    }

    @Override
    public FrameDetailVo detail(Long serialNumber) {
        FrameEntity frameEntity = getOne(new QueryWrapper<FrameEntity>().eq("serial_number", serialNumber));
        Integer modelId = frameEntity.getModelId();
        FrameModelEntity frameModelEntity = frameModelService.getById(modelId);
        FrameDetailVo frameDetailVo = new FrameDetailVo();
        frameDetailVo.setModel(frameModelEntity.getModel());
        MemberEntity memberEntity = memberService.getMemberInfoBySerialNumber(serialNumber);
        frameDetailVo.setUserName(memberEntity.getUserName());
        frameDetailVo.setLocation(memberEntity.getCountry());
        frameDetailVo.setSerialNumber(serialNumber);
        return frameDetailVo;
    }

}
